<?

if ($DATA['component']['id']) {

	// image editor

	$_id_component	= $DATA['component']['id'];			//	list id value
	$_id_data		= D_MB_DATA;			//	meta id value
	$_id_meta		= D_MB_META;			//	meta id value


	$_crit = "id_data=$_id_data AND id_parent=$_id_component";

	$_param			= 'edit';				//	ID param name in the URL, used here & @ template
	$_values			= array('text', 'value');	// which fields are stored in language variations



	$_multilang			= array('text', 'value');	// which fields are stored in language variations
	$_singlelang		= array();	// which fields are stored without language variations


	$_id_gallery	= D_GALLERY_DATA;
	$_gallery		= (int) param('gallery');


	$s = array_extend( array(
		'path' => 'catalog', 'w' => 128, 'h' => 96, 'param' => 'id', 'caption' => '', 'lw' => 400, 'lh' => 300,
		'gal_path' => 'catalog/gal', 'gal_tn' => FP_TN_DIR, 'gal_w' => 800, 'gal_h' => 600, 'gal_tnw' => 128, 'gal_tnh' => 96
	), $DATA['component']['settings']);
	
	$s['gal_path'] = createdir(FP_UPLOAD_PATH."$s[gal_path]-$_gallery/");
	$s['gal_w'] = (int)$s['gal_w'];
	$s['gal_h'] = (int)$s['gal_h'];
	$s['gal_tnw'] = (int)$s['gal_tnw'];
	$s['gal_tnh'] = (int)$s['gal_tnh'];




	if ($a = addslashes(param($_param))) {
		
		switch (action(array('editor_save'))) {
		
			case 'editor_save':





				$_record = array('id_data' => $_id_data, 'key' => $s['gal_path'], 'name' => $a);

				foreach($_singlelang as $name)	// store language independent
					$_record[$name] = postdata($name);

				$temp = array();
	
				foreach($DATA['langs'] as $lang) {	// store multilanguage
					$t1 = array_merge($_record, array('assoc' => $lang['text']));
					foreach($_multilang as $name) {
						$t = postdata($name);
						$t1[$name] = $t[$lang['text']];
					}
					$temp[] = $t1;
				}



				dataset('db_delete', tb('data'), "id_data=$_id_gallery AND key=\"{$s['gal_path']}\" AND name=\"$a\"");
				dataset('db_insert', tb('data'), $temp );




/*
					
				$dat = array('id_data' => $_id_data, 'key' => $s['gal_path'], 'name' => $a);
				foreach ($_values as $v) $dat[$v] = postdata($v);
	
				dataset('db_delete', tb('data'), "id_data=$_id_gallery AND key=\"{$s['gal_path']}\" AND name=\"$a\"");
				dataset('db_insert', tb('data'), $dat );
*/
				takeImage('image', $s['gal_w'], $s['gal_h'], $s['gal_path'].$a, $s['gal_tnw'], $s['gal_tnh'], $s['gal_path'].$s['gal_tn'].$a );
					
				return _transformURL($CURRENT->url, array('remove_param' => $_param));	// end of component edition
				break;
		}
		
		$DATA['object'] = array('value' => '', 'text' => '', 'image' => checkImage($s['gal_path'], $a));

		if ($descr = datafetch_array('db_list', array('table' => tb('data'), 'where' => "id_data=$_id_gallery AND key=\"{$s['gal_path']}\" AND name=\"$a\""))) {
			if ($descr[0]['assoc'])
				foreach ($descr as $v)
					foreach ($_multilang as $m)
						$DATA['object']['data'][$v['assoc']][$m] = $v[$m];
			else
				foreach ($_multilang as $m)
					$DATA['object']['data'][LANG_DEF][$m] = $descr[0][$m];
			
//			$DATA['object']['value'] = $descr['value'];
//			$DATA['object']['text'] = $descr['text'];
		}
	}

	return false;
	
} else exit;

?>